/*
 * Copyright (c) 2010 Andy Aspell-Clark
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package uk.org.aspellclark.gedcom.controller;

import java.util.ArrayList;
import java.util.List;

import uk.org.aspellclark.gedcom.model.ChildToFamilyLink;
import uk.org.aspellclark.gedcom.model.SpouseToFamilyLink;
import uk.org.aspellclark.gedcom.model.records.FamRecord;
import uk.org.aspellclark.gedcom.model.records.IndividualRecord;

import com.db4o.ObjectSet;

public class IndividualController extends AbstractDB4OController {
	/* {version=1.0.0.00, author=Andy Aspell-Clark, src_path=F:\Android\Workspace\GedcomLibrary\src, since=May 2010}*/

	/**
	 * Constructor
	 * @param dbCntrlr
	 */
	public IndividualController(Db4oController dbCntrlr) {
		super(dbCntrlr);
	}

	/**
	 * @param ind
	 */
	public void addIndividual(IndividualRecord ind) {
		this.dbCntrlr.db.store(ind);
	}

	/**
	 * @param indiv
	 * @return
	 */
	public List<String> getChildrenXrefs(IndividualRecord indiv) {
		if (indiv.spouseLink != null) {
			SpouseToFamilyLink famXref = indiv.spouseLink.get(0);
			FamilyController fc = new FamilyController(this.dbCntrlr);
			FamRecord fr = fc.getFamily(famXref.famsXRef);
			if (fr != null) {
				return fr.childrenXRef;
			}
		}
		return null;
	}

	public IndividualRecord getFather(IndividualRecord indiv) {
		if (indiv.childLink != null) {
			ChildToFamilyLink famXref = indiv.childLink.get(0);
			FamilyController fc = new FamilyController(this.dbCntrlr);
			FamRecord fr = fc.getFamily(famXref.famcXRef);
			if (fr != null) {
				return this.getIndividual(fr.husb);
			}
		}
		return null;
	}

	public IndividualRecord getIndividual(final String xref) {
		IndividualRecord ind = new IndividualRecord(xref);
		ObjectSet<IndividualRecord> retrndObjs = this.dbCntrlr.db.queryByExample(ind);

		if (retrndObjs.size() == 1) {
			ind = retrndObjs.next();
		} else {
			ind = null;
		}
		return ind;
	}

	public List<IndividualRecord> getIndividuals() {
		List<IndividualRecord> objList = this.dbCntrlr.db.queryByExample(IndividualRecord.class);
		return objList;
	}

	/**
	 * @param childrenIds
	 * @return
	 */
	public List<IndividualRecord> getIndividuals(List<String> childrenIds) {
		List<IndividualRecord> children = new ArrayList<IndividualRecord>();
		for (String xref : childrenIds) {
			children.add(this.getIndividual(xref));
		}
		return null;
	}

	public IndividualRecord getMother(IndividualRecord indiv) {
		if (indiv.childLink != null) {
			ChildToFamilyLink famXref = indiv.childLink.get(0);
			FamilyController fc = new FamilyController(this.dbCntrlr);
			FamRecord fr = fc.getFamily(famXref.famcXRef);
			if (fr != null) {
				return this.getIndividual(fr.wife);
			}
		}
		return null;
	}

	public ArrayList<IndividualRecord> getSiblings(String xref) {
		ArrayList<IndividualRecord> objList = new ArrayList<IndividualRecord>();
		IndividualRecord indiv = this.getIndividual(xref);

		if (indiv.childLink != null) {
			ChildToFamilyLink famXref = indiv.childLink.get(0);
			FamilyController fc = new FamilyController(this.dbCntrlr);
			return fc.getChildren(famXref.famcXRef);

		}
		return objList;
	}

	public IndividualRecord getSpouse(IndividualRecord indiv) {
		if (indiv.spouseLink != null) {
			SpouseToFamilyLink famXref = indiv.spouseLink.get(0);
			FamilyController fc = new FamilyController(this.dbCntrlr);
			FamRecord fr = fc.getFamily(famXref.famsXRef);
			if (fr != null) {
				if (fr.wife.equals(indiv.xref)) {
					return this.getIndividual(fr.husb);
				}
				return this.getIndividual(fr.wife);
			}
		}
		return null;
	}
}
